package com.wf.demo.info.web;


import com.wf.demo.info.dao.DeviceDAO;
import com.wf.demo.info.pojo.Alarm;
import com.wf.demo.info.service.AlarmService;
import com.wf.demo.info.service.DeviceService;
import com.wf.demo.info.util.Page4Navigator;
import com.wf.demo.sys.annotation.SysLog;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController

public class AlarmController {
    @Autowired
    AlarmService alarmService;
    @Autowired
    DeviceDAO deviceDAO;
    @GetMapping("/alarms")
    @RequiresPermissions("info:alarm")
    public Page4Navigator<Alarm> list( @RequestParam(value = "start",defaultValue = "0")int start,
                                      @RequestParam(value = "size",defaultValue = "15")int size) throws Exception{
        start = start<0? 0:start;
        Page4Navigator<Alarm> page = alarmService.list(start,size,5);
        return page;
    }
    @GetMapping("/devices/{did}/alarms")
    @RequiresPermissions("info:alarm")
    public Page4Navigator<Alarm> list(@PathVariable("did")Long did, @RequestParam(value = "start",defaultValue = "0")int start,
                                      @RequestParam(value = "size",defaultValue = "5")int size) throws Exception{
        start = start<0? 0:start;
        Page4Navigator<Alarm> page = alarmService.list(did,start,size,5);
        return page;
    }
    @GetMapping("/alarms/{id}")
    @RequiresPermissions("info:alarm")
    public Object get(@PathVariable("id")Long id){
        Alarm bean= alarmService.get(id);
        return  bean;
    }
    //故障记录自动生成，所有这里不需要修改和增加的方法。
    @PostMapping("/alarms")
    @RequiresPermissions("info:alarm")
    public Object add(@RequestBody Alarm bean){
          alarmService.add(bean);
          return  bean;
    }
    @SysLog("删除报警")
    @DeleteMapping("/alarms/{id}")
    @RequiresPermissions("info:alarm")
    public String delete(@PathVariable("id")Long id){
        alarmService.delete(id);
        return  null;
    }
    //根据控制系统编号搜索得到设备的报警记录
    @GetMapping("/devices/alarms")
    @RequiresPermissions("info:alarm")
    public Page4Navigator<Alarm> searchByCsNumber(String str, @RequestParam(value = "start",defaultValue = "0")int start,
                                      @RequestParam(value = "size",defaultValue = "15")int size) throws Exception{
        //根据控制系统编号搜索得到device ID
        long did =deviceDAO.findByCsNumber(str).getId();
        start = start<0? 0:start;
        Page4Navigator<Alarm> page = alarmService.list(did,start,size,5);
        return page;
    }
}
